package com.uls.web.controller.ycl;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.system.service.impl.SysUserServiceImpl;
import com.uls.ycl.domain.RepairRequest;
import com.uls.ycl.mapper.RepairRequestMapper;
import com.uls.ycl.service.IRepairRequestService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.RepairEvaluation;
import com.uls.ycl.service.IRepairEvaluationService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 报修评价Controller
 * 
 * @author ycl
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/ycl/evaluation")
public class RepairEvaluationController extends BaseController
{
    @Autowired
    private IRepairEvaluationService repairEvaluationService;

    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private IRepairRequestService repairRequestService;
    @Autowired
    private RepairRequestMapper repairRequestMapper;

    /**
     * 查询报修评价列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(RepairEvaluation repairEvaluation)
    {
        startPage();
        List<RepairEvaluation> list = repairEvaluationService.selectRepairEvaluationList(repairEvaluation);

        for(RepairEvaluation evaluation : list){
            Long userId = evaluation.getUserId();
            SysUser sysUser = sysUserService.selectUserById(userId);
            if(sysUser!=null)
                evaluation.setUserName(sysUser.getUserName());
        }

        return getDataTable(list);
    }

    /**
     * 导出报修评价列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:export')")
    @Log(title = "报修评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairEvaluation repairEvaluation)
    {
        List<RepairEvaluation> list = repairEvaluationService.selectRepairEvaluationList(repairEvaluation);
        ExcelUtil<RepairEvaluation> util = new ExcelUtil<RepairEvaluation>(RepairEvaluation.class);
        util.exportExcel(response, list, "报修评价数据");
    }

    /**
     * 获取报修评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:query')")
    @GetMapping(value = "/{evaluationId}")
    public AjaxResult getInfo(@PathVariable("evaluationId") Long evaluationId)
    {
        return success(repairEvaluationService.selectRepairEvaluationByEvaluationId(evaluationId));
    }

    /**
     * 新增报修评价
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:add')")
    @Log(title = "报修评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairEvaluation repairEvaluation)
    {


        return toAjax(repairEvaluationService.insertRepairEvaluation(repairEvaluation));
    }

    /**
     * 修改报修评价
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:edit')")
    @Log(title = "报修评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepairEvaluation repairEvaluation)
    {
        repairEvaluation.setEvaluatedAt(new Date());
        repairEvaluation.setStatus(1);
        //同时评价后显示已评价
        RepairRequest request = repairRequestService.selectRepairRequestByRequestId(repairEvaluation.getRequestId());
        request.setStatus(8);
        repairRequestMapper.updateRepairRequest(request);

        return toAjax(repairEvaluationService.updateRepairEvaluation(repairEvaluation));
    }

    /**
     * 删除报修评价
     */
    @PreAuthorize("@ss.hasPermi('ycl:evaluation:remove')")
    @Log(title = "报修评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{evaluationIds}")
    public AjaxResult remove(@PathVariable Long[] evaluationIds)
    {
        return toAjax(repairEvaluationService.deleteRepairEvaluationByEvaluationIds(evaluationIds));
    }
}
